package com.nuoniu.sibanyun.service.erp.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.erp.ErpVoucherClass;
import com.nuoniu.sibanyun.entity.finance.VAccountingVoucherMain;
import com.nuoniu.sibanyun.mapper.erp.ErpVoucherClassMapper;
import com.nuoniu.sibanyun.service.erp.IErpAccountService;
import com.nuoniu.sibanyun.service.erp.IErpVoucherClassService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 凭证类别 服务实现类
 * </p>
 *
 * @author 小啊刚
 * @since 2021-03-31
 */
@Service
public class ErpVoucherClassServiceImpl extends ServiceImpl<ErpVoucherClassMapper, ErpVoucherClass> implements IErpVoucherClassService {

    @Autowired
    private IErpAccountService erpAccountService;

    @Override
    public List<ErpVoucherClass> getDefaultList(Integer classMethod) {
        return baseMapper.getDefaultList(classMethod);
    }

    @Override
    public List<ErpVoucherClass> getList() {
        UserInfo userInfo = UserThreadLocal.get();
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        return list(wrapper);
    }

    @Override
    public void saveVoucherClass(List<ErpVoucherClass> voucherClass) {
        UserInfo userInfo = UserThreadLocal.get();
        List<Long> detailIds = new ArrayList<>();
        voucherClass.forEach(v -> {
            if(StringUtils.isBlank(v.getClassName())) {
                throw new NuoNiuException("类别名称不能为空");
            }
            if(StringUtils.isBlank(v.getClassWord())) {
                throw new NuoNiuException("类别字不能为空");
            }

            if(v.getId() != null) {
                detailIds.add(v.getId());
            }else {
                v.setAccountId(erpAccountService.getCurrentAccountId());
                v.setCompanyId(userInfo.getCompanyId());
            }
        });

        //删除多余数据
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        if(detailIds != null && detailIds.size() > 0) {
            wrapper.notIn("id", detailIds);
            this.remove(wrapper);
        }else {
            this.remove(wrapper);
        }
        //保存
        saveOrUpdateBatch(voucherClass);
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        UserInfo userInfo = UserThreadLocal.get();
        for(Long id : ids) {
            List<VAccountingVoucherMain> voucherMains = baseMapper.getByVClass(id, userInfo.getCompanyId());
            if(voucherMains.size() > 0) {
                throw new NuoNiuException("已在凭证中使用,不能删除");
            }
        }

        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("company_id", userInfo.getCompanyId());
        wrapper.in("id", ids);
        this.remove(wrapper);
    }
}
